找传奇、传世资源到传世资源站!

C# 操作xml文件(创建、修改、保存等编辑操作)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard
from clipboardusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Runtime.InteropServices;using System.Collections;using System.Xml;namespace FileOperate{ public partial class Form1 : Form { public Form1() { dtbfinal.Columns.Add("id", typeof(string)); InitializeComponent(); } #region 变量声明区 public string str = "";//该变量保存INI文件所在的具体物理位置 public string strOne = ""; string filePath = "";//文件路径 OpenFileDialog ofd = new OpenFileDialog();//对话框 SaveFileDialog save = new SaveFileDialog(); DataTable dtb = new DataTable();//全局表 DataTable dtbfinal = new DataTable();//全局表 TextBox textbox = new TextBox(); List<string> temp = new List<string>(); List<string> listNew = new List<string>();//全局 List List<string> list = new List<string>(); List<string> listNew1 = new List<string>(); #endregion #region 创建节点 public void CreateNode(XmlDocument xmldoc, XmlNode parentnode, string name, string value) { XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, name, null); node.InnerText = value; parentnode.AppendChild(node); } #endregion #region 打开 private void button2_Click(object sender, EventArgs e) { dtb.Clear(); listBox1.Items.Clear(); ofd.Multiselect = false; ofd.Title = "选择XML文件"; ofd.Filter = "XML文件(*.XML)|*.XML"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { listBox1.Items.Clear(); dtbfinal.Clear(); if (ofd.FileName.ToString().Trim().Equals("")) { return; } else { filePath = ofd.FileName; str = filePath; if (File.Exists(filePath)) { try { List<Model> ModeList = new List<Model>(); XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true;//忽略文档里面的注释 using (XmlReader reader = XmlReader.Create(str, settings)) { doc.Load(reader); XmlNode xn = doc.SelectSingleNode("ConnectString"); // 得到根节点的所有子节点 XmlNodeList xnl = xn.ChildNodes; int i = 0; foreach (XmlNode xn1 in xnl) { DataRow dr = dtbfinal.NewRow(); Model Model = new Model(); // 将节点转换为元素,便于得到节点的属性值 XmlElement xe = (XmlElement)xn1; // 得到Type和ISBN两个属性的属性值 Model.Num = xe.GetAttribute("ISBN").ToString(); ModeList.Add(Model); dr[0] = Model.Num; this.dtbfinal.Rows.Add(dr); } } ModeList.Add(new Model()); foreach (DataRow row in dtbfinal.Rows) { string value = row[0].ToString(); listBox1.Items.Add(value); } int x; string[] arrayA = new string[dtbfinal.Rows.Count]; for (x = 0; x < dtbfinal.Rows.Count; x ) { DataRow dr2 = dtbfinal.Rows[x]; arrayA[x] = Convert.ToString(dr2["id"]); TextBox textbox = new TextBox(); textbox.Name = "tb_" x; textbox.Text = dtbfinal.Rows[x]["id"].ToString(); textbox.Location = new Point(12, 15 x * 30); this.groupBox2.Controls.Add(textbox); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } } #endregion #region 上移 private void button3_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; if (index == -1)//没选中 { return; } else if(index > 0) { string str = listBox1.Items[index].ToString(); listBox1.Items.RemoveAt(index); listBox1.Items.Insert(index - 1, str); listBox1.SelectedIndex = index - 1; } } #endregion #region 下移 private void button4_Click(object sender, EventArgs e) { int index = listBox1.SelectedIndex; if (index == -1) //没选中 {return; } else if (index<listBox1.Items.Count-1) { string str = listBox1.Items[index].ToString(); listBox1.Items.RemoveAt(index); listBox1.Items.Insert(index 1, str); listBox1.SelectedIndex = index 1; } } #endregion #region 保存 private void button5_Click(object sender, EventArgs e) { listNew1.Clear(); XmlDocument xmldoc = new XmlDocument(); XmlNode node; node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); xmldoc.AppendChild(node); XmlNode root = xmldoc.CreateElement("ConnectString"); xmldoc.AppendChild(root); foreach (Control c in groupBox2.Controls) { if (c is TextBox) { listNew1.Add(c.Text.ToString()); } } foreach (var item in listNew1) { XmlElement Item = xmldoc.CreateElement("book"); string name = "ISBN"; Item.SetAttribute(name, item.ToString()); root.AppendChild(Item); } try { xmldoc.Save(filePath); MessageBox.Show("修改成功!"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 创建 private void button6_Click(object sender, EventArgs e) { try { list.Clear(); ofd.Filter = "文本文件(*.txt*)|*.txt*"; ofd.FilterIndex = 1; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { string localFilePath = ofd.FileName.ToString(); //获得文件路径 //string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") 1); //获取文件名,不带路径 string[] lines = File.ReadAllLines(localFilePath, System.Text.Encoding.UTF8); XmlDocument xmldoc = new XmlDocument(); XmlNode node; node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null); xmldoc.AppendChild(node); XmlNode root = xmldoc.CreateElement("ConnectString"); xmldoc.AppendChild(root); foreach (var item in lines) { listNew.Add(item.ToString()); } foreach (var item in listNew) { XmlElement Item = xmldoc.CreateElement("book"); string name = "ISBN"; Item.SetAttribute(name, item.ToString()); root.AppendChild(Item); } save.Filter = "文件(*.xml*)|*.xml*"; save.FilterIndex = 1; save.RestoreDirectory = true; if (save.ShowDialog() == DialogResult.OK) { localFilePath = save.FileName.ToString(); xmldoc.Save(localFilePath); MessageBox.Show("创建XML文件成功!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion #region 获取 private void button1_Click_1(object sender, EventArgs e) { temp.Clear(); foreach (string b in listBox1.Items) { temp.Add(b); } } #endregion }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复